package logic

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"order_srv/internal/model"

	"order_srv/internal/svc"
	"order_srv/order_proto"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderDetailLogic {
	return &OrderDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderDetailLogic) OrderDetail(in *order_proto.OrderDetailRequest) (*order_proto.OrderInfo, error) {
	// todo: add your logic here and delete this line
	var order model.Order
	tx := model.DB.Where(&model.Order{OrderSn: in.OrderSn}).First(&order)
	if tx.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "订单不存在")
	}

	return &order_proto.OrderInfo{
		Id:      int32(order.ID),
		UserId:  order.UserId,
		GoodsId: order.GoodsId,
		Price:   order.Price,
		PayType: order.PayType,
		Status:  order.Status,
		OrderSn: order.OrderSn,
		TradeNo: order.TradeNo,
		Num:     order.Num,
	}, nil
}
